package com.asset.salesforce.validator;

import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;

import com.asset.salesforce.domain.StockCategory;
import com.asset.salesforce.form.StockCategoryForm;
import com.asset.salesforce.service.InventoryService;

public class CategoryCodeNotExistConstraintValidator implements ConstraintValidator<CategoryCodeNotExist, Object>{
@Autowired
private InventoryService inventoryService;
	
@Override
public void initialize(CategoryCodeNotExist arg0) {
	// TODO Auto-generated method stub
	
}

@Override
public boolean isValid(Object arg0, ConstraintValidatorContext arg1) {
	StockCategoryForm categoryForm = (StockCategoryForm)arg0;
	boolean success = false; 	
	List<StockCategory> stockCategories = inventoryService.getAllCategoriesByEmployeeId(categoryForm.getEmpId());
	if(stockCategories.size()==0){
		success=true;
	}
	else{
	for (StockCategory stockCategory : stockCategories) {
		if(!stockCategory.getCode().equals(categoryForm.getCode())){
			success=true;
		}else{
			success=false;
		}
	}
	}
	return success;
   }
}
